// index.js
// 获取应用实例
import {Request} from "../../utils/request"
const app = getApp()
console.log(app.globalData);
Page({
  data: {
    userInfo:app.globalData.userInfo,
    list:[]
  },
  getexchangequery(){
    Request({
      api:'/mss/lottery/exchange/query',
      data:{
        prize_states:['已使用'],
        k:'',
        count:20000,
        offset:0,
      },
      method:'post',
    }).then((res)=>{
        let data = res.data;
        data.map((e,i)=>{
          var str1 = e['phone'];
          var reg = /^(\d{3})\d*(\d{4})$/;
          var str2 = str1.replace(reg,'$1****$2')
          data[i]['phone'] = str2;
        });
        this.setData({
          list:data
        })
    })
  },
  scan(){
    wx.downloadFile({
      url: 'https://cw.dingplayer.com/sys/hexiao.txt',
      success (res) {
        // 只要服务器有响应数据，就会把响应内容写入文件并进入 success 回调，业务需要自行判断是否下载到了想要的内容
        if (res.statusCode === 200) {
          console.log(res);
          const fs = wx.getFileSystemManager()
          let content = fs.readFileSync( res.tempFilePath,'utf-8',0)
          console.log(content)
          if(content=='1'){
            if(app.globalData.userInfo){
              wx.scanCode({
                onlyFromCamera: true,
                success (res) {
                  wx.navigateTo({
                    url: '/pages/jpdetail/index?id='+res.result,
                  })
                }
              })
            }else{
              wx.navigateTo({
                url: '/pages/login/index',
              })
            }
          }else{
            wx.showToast({
              title: '核销已结束',
              icon:'error'
            })
          }
        }
      }
    })
   
  },
  toSetting(){
    wx.navigateTo({
      url: '/pages/setting/index',
    })
  },
  onLoad() {
    if (wx.getUserProfile) {
      this.setData({
        canIUseGetUserProfile: true
      })
    }
    this.setData({
      userInfo:app.globalData.userInfo,
    })
    
  },
  onShow(){
    this.getexchangequery();
  }
})
